package com.airport.ticket.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.airport.ticket.common.UrlUtil;
import com.airport.ticket.dao.FeedbackMapper;
import com.airport.ticket.entity.Feedback;
import com.airport.ticket.entity.FeedbackExample;
import com.airport.ticket.entity.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/admin")
public class FeedbackController {
	
	@Autowired
	private FeedbackMapper feedbackMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(value = "/feedback_list.html")
    public String feedbackList(ModelMap map,String keyword, String pageNow, String pageSize, String errorMsg){
		
		map.addAttribute("nav", "4");
		map.addAttribute("title", "留言板");
		map.addAttribute("keyword", keyword);
		
		if(errorMsg!=null && !"".equals(errorMsg)) {
			map.addAttribute("errorMsg", errorMsg);
		}
		
		if(pageNow==null || "".equals(pageNow) || Integer.parseInt(pageNow) < 1) {
			pageNow = "1";
		}
		
		if(pageSize==null || "".equals(pageSize) || Integer.parseInt(pageSize) < 1) {
			pageSize = "20";
		}
		
		FeedbackExample condition = new FeedbackExample();
		if(keyword!=null && !"".equals(keyword)) {
			condition.createCriteria().andRemarkLike(keyword);
		}
		condition.setOrderByClause("id DESC");
		PageHelper.startPage(Integer.parseInt(pageNow),Integer.parseInt(pageSize));
		List<Feedback> resultList = feedbackMapper.selectByExample(condition);
		
		if(resultList==null || resultList.size()==0){
			map.addAttribute("retCode", "99");
			map.addAttribute("pageNow", pageNow);
			map.addAttribute("pageSize", pageSize);
			map.addAttribute("totalPage", "0");
			return "admin/feedback/feedback_list";
        }
		
		PageInfo<Feedback> pageInfo = new PageInfo<>(resultList);
		map.addAttribute("pageNow", pageNow);
		map.addAttribute("pageSize", pageSize);
		map.addAttribute("totalPage", pageInfo.getPages());
		map.addAttribute("resultList", resultList);
		
		return "admin/feedback/feedback_list";
    }
	
	
	@RequestMapping(value = "/add_feedback.html")
    public String addFeedback(ModelMap map){

		map.addAttribute("nav", "4");
		map.addAttribute("title", "新增留言");		
		return "admin/feedback/add_feedback";
    }
	
	@PostMapping(value = "/do_add_feedback.html")
    public String doAddFeedback(ModelMap map, Feedback info){
		map.addAttribute("nav", "4");
		map.addAttribute("title", "新增留言");	
				
		HttpSession session = request.getSession();
		UserInfo loginUserInfo = (UserInfo)session.getAttribute("loginUserInfo");
		
		/**
		 * 插入留言表
		 */
		info.setUserId(loginUserInfo.getId());
		info.setCreateTime(new Date());
		feedbackMapper.insert(info);
		
		return "redirect:/admin/feedback_list.html";
    }
	
	@RequestMapping(value = "/do_delete_feedback.html")
    public String doDeleteNotice(ModelMap map, Feedback info){
		map.addAttribute("nav", "4");
		map.addAttribute("title", "删除留言");
		
		/**
		 * 判断留言是否存在
		 */
		Feedback feedbackInfo = feedbackMapper.selectByPrimaryKey(info.getId());
		if(feedbackInfo==null || feedbackInfo.getId()==null || "".equals(feedbackInfo.getId())) {
			return "redirect:/admin/feedback_list.html?errorMsg="+UrlUtil.parseUtf8("留言不存在");
		}
		
		
		/**
		 * 删除留言
		 */
		feedbackMapper.deleteByPrimaryKey(info.getId());
		
		return "redirect:/admin/feedback_list.html";
    }
	
}
